package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyang.constant.ResultCode;
import com.yiyang.domain.dto.BedDetailsDto;
import com.yiyang.domain.po.Bed;
import com.yiyang.domain.po.BedDetails;
import com.yiyang.domain.po.Customer;
import com.yiyang.domain.vo.BedDetailVo;
import com.yiyang.exception.ServiceException;
import com.yiyang.mapper.BedMapper;
import com.yiyang.mapper.CustomerMapper;
import com.yiyang.service.BedDetailsService;
import com.yiyang.mapper.BedDetailsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiyang.domain.ResultPage;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @author 31044
* @description 针对表【bed_details】的数据库操作Service实现
* @createDate 2024-09-28 15:04:24
*/
@Service
public class BedDetailsServiceImpl extends ServiceImpl<BedDetailsMapper, BedDetails>
    implements BedDetailsService {

    @Autowired
    private BedDetailsMapper bedDetailsMapper;
    @Autowired
    private BedMapper bedMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResultPage<BedDetailVo> listBedDetailVoPage(Integer page, Integer pageSize, String customerName, String endDate, String startDate, Integer isDeleted) {

        Page<BedDetailVo> voPage = new Page<>(page, pageSize);

        bedDetailsMapper.selectPageByCondition(voPage, customerName, startDate, endDate, isDeleted);

        return new ResultPage<BedDetailVo>(voPage.getTotal(),voPage.getRecords());
    }

    @Override
    @Transactional
    public void exchangeBed(BedDetailsDto bedDetailsDto) throws ParseException {
        //1检查新床位是否是空闲状态
       Bed newBed =  bedMapper.selectById(bedDetailsDto.getNewBedId());
        if (newBed.getStatus() == 2){
            throw new ServiceException(ResultCode.ERROR_EXCHANGE_BED,"床位被占用");
        }

        //2修改原来客户的床位信息（状态和结束时间）
        BedDetails oldbedDetails = new BedDetails();
        oldbedDetails.setId(bedDetailsDto.getId());
        oldbedDetails.setIsDelete(1);
        oldbedDetails.setEndDate(new Date());
        Integer row1 = bedDetailsMapper.updateById(oldbedDetails);

        //3添加新的床位信息

        BedDetails newbedDetails = new BedDetails();
        newbedDetails.setStartDate(new Date());
        newbedDetails.setEndDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(bedDetailsDto.getEndDate()));
        newbedDetails.setBedDetails(bedDetailsDto.getBuildingNo()+"#"+newBed.getBedNo());
        newbedDetails.setCustomerId(bedDetailsDto.getCustomerId());
        newbedDetails.setBedId(newBed.getId());

        Integer row2 = bedDetailsMapper.insert(newbedDetails);

        //4新床位变更为占用状态
        newBed.setStatus(2);
        Integer row3 = bedMapper.updateById(newBed);
        Bed oldBed = bedMapper.selectById(bedDetailsDto.getOldBedId());
        oldBed.setStatus(1);
        Integer row4 = bedMapper.updateById(oldBed);

        //5更新客户变更的楼号 房间号 床位号

        Customer customer = customerMapper.selectById(bedDetailsDto.getCustomerId());
        customer.setRoomNo(bedDetailsDto.getNewRoomNo());
        customer.setBuildingNo(bedDetailsDto.getBuildingNo());
        customer.setBedId(bedDetailsDto.getNewBedId());

        Integer row5 = customerMapper.updateById(customer);

        if (row1 == 0 || row2 == 0 || row3 == 0 || row4 == 0 || row5 == 0){

            throw new RuntimeException("更换床位失败");
        }
    }
}




